package cn.shop.dys.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.shop.dys.R;
import cn.shop.dys.bean.ProductInfo;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.dys.widget.ProductImageView;
import cn.shop.lib_common.CommonViewHolder;

/**
 * 作者：jian on 2016/4/21 15:14
 * 孙建
 */
public class SearchFailAdapter extends BaseAdapter{
    private List<ProductInfo> mProductInfos;
    private Context mContext;
    private BitmapUtils mBitmapUtils;
    private int[] randomNumber=new int[CommonConstant.RECOMMEND_SEARCH_COUNT];;
    private int mCount;
    public SearchFailAdapter(List<ProductInfo> mProductInfos, Context mContext) {
        this.mProductInfos = mProductInfos;
        this.mContext = mContext;
        mBitmapUtils = new BitmapUtils(mContext);
    }
    /*private void getRandomNumber(){
        Random random=new Random();
        randomNumber[0] = random.nextInt(mCount);
        for (int i = 1; i < mCount; i++) {
            randomNumber[i]=random.nextInt(mCount);
            for (int j =1; j < i; j++) {
                while(randomNumber[i]==randomNumber[j]){
                    i--;
                }
            }
        }
    }*/
    public static int[] randomArray(int min,int max,int n){
        int len = max-min+1;

        if(max < min || n > len){
            return null;
        }

        //初始化给定范围的待选数组
        int[] source = new int[len];
        for (int i = min; i < min+len; i++){
            source[i-min] = i;
        }

        int[] result = new int[n];
        Random rd = new Random();
        int index = 0;
        for (int i = 0; i < result.length; i++) {
            //待选数组0到(len-2)随机一个下标
            index = Math.abs(rd.nextInt() % len--);
            //将随机到的数放入结果集
            result[i] = source[index];
            //将待选数组中被随机到的数，用待选数组(len-1)下标对应的数替换
            source[index] = source[len];
        }
        return result;
    }
    @Override
    public int getCount() {
        if(mProductInfos==null){
            mCount= 0;
        }else if(mProductInfos.size()<=CommonConstant.RECOMMEND_SEARCH_COUNT){
            mCount= mProductInfos.size();
        }else {
            mCount= CommonConstant.RECOMMEND_SEARCH_COUNT;
        }
        Log.d("test","mProductInfos.size()"+mProductInfos.size());
        randomNumber=randomArray(0,mCount-1,mCount);
       // getRandomNumber();
        return mCount;
    }

    @Override
    public Object getItem(int position) {
        return mProductInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommonViewHolder holder = CommonViewHolder.getCommonViewHolder(convertView, mContext, R.layout.search_item_fail_recommend_prodect);
        ProductInfo productInfo = mProductInfos.get(randomNumber[position]);
        ProductImageView view = holder.getView(R.id.piv_fail, ProductImageView.class);
        view.setText((int)productInfo.getPrice()+"");
        String pic = CommonConstant.HOST+productInfo.getPic();
        mBitmapUtils.display(view,pic);
        return holder.convertView;
    }
}
